package com.liuwei.spring.cloud.junit.leetcode.substring;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class SubContinuous {

    /**
     * 给定一个字符串，只包含字母和数字，按要求找出字符串中的最长（连续）子串的长度，字符串本身是其最长的子串;
     * 子串要求： 1、 只包含1个字母(a~z, A~Z)，其余必须是数字；
     *          2、 字母可以在子串中的任意位置；
     * 如果找不到满足要求的子串，如全是字母或全是数字，则返回-1。
     * @param args
     */
    public static void main(String[] args) {
        List<String> values = Arrays.asList("a","1","31","5","7","28","d","e");
        int max = 0;
        for (int i=0;i<values.size();i++){
            Step nStep = getMaxLengthEndAtIndex(values,i);
            if(nStep.getMaxLength()>=max){
                max = nStep.getMaxLength();
            }
        }
        log.info("max:{}",max);
    }

    private static Step getMaxLengthEndAtIndex(List<String> values, int n){
        boolean isNumber =false;
        try {
            Integer.parseInt(values.get(n));
            isNumber = true;
        } catch (NumberFormatException e) {
            isNumber = false;
        }
        if(n==0){
            Step step = new Step();
            step.setMaxLength(1);
            if(isNumber){
                step.setLastNumber(Integer.parseInt(values.get(n)));
                step.setContainsAz(false);
            }else{
                step.setLastNumber(null);
                step.setContainsAz(true);
            }
            return step;
        }
        Step beforeStep = getMaxLengthEndAtIndex(values,n-1);
        if(isNumber){
            Integer currentNumber = Integer.parseInt(values.get(n));
            Integer lastNumber = beforeStep.getLastNumber();
            if(lastNumber == null){
                Step step = new Step();
                step.setMaxLength(beforeStep.getMaxLength()+1);
                step.setLastNumber(currentNumber);
                step.setContainsAz(beforeStep.isContainsAz());
                return step;
            }else{
                if(currentNumber>lastNumber){
                    Step step = new Step();
                    step.setMaxLength(beforeStep.getMaxLength()+1);
                    step.setLastNumber(currentNumber);
                    step.setContainsAz(beforeStep.isContainsAz());
                    return step;
                }else{
                    Step step = new Step();
                    step.setMaxLength(1);
                    step.setLastNumber(currentNumber);
                    step.setContainsAz(false);
                    return step;
                }
            }
        }else{
            boolean containsAz = beforeStep.isContainsAz();
            if(containsAz){
                Step step = new Step();
                step.setMaxLength(1);
                step.setLastNumber(null);
                step.setContainsAz(true);
                return step;
            }else{
                Step step = new Step();
                step.setMaxLength(beforeStep.getMaxLength()+1);
                step.setLastNumber(null);
                step.setContainsAz(true);
                return step;
            }
        }
    }

    @Data
    public static class Step{
        int maxLength;
        Integer lastNumber = null;
        boolean containsAz = false;
    }
}
